package xyz.liuz.edb.heart;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.timeout.IdleStateHandler;
import xyz.liuz.edb.heart.protocol.MessageProtocolDecoder;
import xyz.liuz.edb.heart.protocol.MessageProtocolEncoder;

import java.util.concurrent.TimeUnit;

public class HeartServerChannelInitializer extends ChannelInitializer<SocketChannel> {
    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        pipeline.addLast("MessageProtocolEncoder",new MessageProtocolEncoder());
        pipeline.addLast("MessageProtocolDecoder", new MessageProtocolDecoder());
        // 添加读心跳 10s 没有读操作则触发读空闲事件
        pipeline.addLast("ReadIdleStateHandler", new IdleStateHandler(10, 0, 0, TimeUnit.SECONDS));
        pipeline.addLast("HeartServerHandler", new HeartServerHandler());
    }
}
